/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package apteka.admin;

import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import apteka.CategoryManager;
import apteka.tables.Category;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Marek
 */

@Controller
@RequestMapping("/admin_panel/categories.htm")
public class AdminCategoryController {

    @Autowired
     CategoryManager cat_man;

     @RequestMapping(method = RequestMethod.GET)
     public String showPage(Map model) {
        model.put("page", "categories.jsp");
        model.put("categoryList", cat_man.getMainCategories());
        addCategory addcat = new addCategory();
        addcat.setIdc(-1);
        model.put("addCategory",addcat);
        return "admin_panel/main";
     }

     @RequestMapping(method = RequestMethod.POST)
     public String addCategory(addCategory category,BindingResult result,Map model) {
         if (!category.getNewCat().isEmpty()) {
            if (category.getIdc()==-1) {
                cat_man.addCategory(category.getCategory(), category.getNewCat());
             } else {
                cat_man.editCategory(category.getIdc(), category.getNewCat());
                cat_man.moveCategory(category.getIdc(), category.getCategory());
             }
            model.put("categoryShownNr",category.getCategory());
            model.put("categoryShown",cat_man.getSubCategories(category.getCategory()));
         }
        model.put("page", "categories.jsp");
        model.put("categoryList", cat_man.getMainCategories());
        addCategory addcat = new addCategory();
        addcat.setIdc(-1);
        model.put("addCategory",addcat);
        return "admin_panel/main";
     }

     @RequestMapping(params="op=delete",method = RequestMethod.GET)
     public String deleteCategory(@RequestParam("id") String id,Map model) {
        try {
            cat_man.deleteCategory(Integer.parseInt(id));
        } catch (Exception e) {
            
        }
        model.put("page", "categories.jsp");
        model.put("categoryList", cat_man.getMainCategories());
        addCategory addcat = new addCategory();
        addcat.setIdc(-1);
        model.put("addCategory",addcat);
        return "admin_panel/main";
    }
        
     @RequestMapping(params="op=show",method = RequestMethod.GET)
     public String showCategory(@RequestParam("id") String id,Map model) {
        try {
            int id_ = Integer.parseInt(id);
            model.put("categoryShownNr",id_);
            model.put("categoryShown",cat_man.getSubCategories(id_));
        } catch (Exception e) {
            
        }
        model.put("page", "categories.jsp");
        model.put("categoryList", cat_man.getMainCategories());
        
        addCategory addcat = new addCategory();
        addcat.setIdc(-1);
        model.put("addCategory",addcat);
        return "admin_panel/main";
     }

     @RequestMapping(params="op=edit",method = RequestMethod.GET)
     public String editCategory(@RequestParam("id") String id,Map model) {
        Category cat=null;
        try {
            int id_ = Integer.parseInt(id);
            cat=cat_man.getCategory(id_);
            if (cat.getCategory().getIdcategory()==0) {
                model.put("categoryShownNr",id_);
                model.put("categoryShown",cat_man.getSubCategories(id_));
            } else {
                model.put("categoryShownNr",cat.getCategory().getIdcategory());
                model.put("categoryShown",cat_man.getSubCategories(cat.getCategory().getIdcategory()));
            }
        } catch (Exception e) {

        }
        addCategory addcat = new addCategory();
        model.put("page", "categories.jsp");
        model.put("categoryList", cat_man.getMainCategories());
        if (cat!=null) {
            model.put("categoryEdit",cat);
            addcat.setIdc(cat.getIdcategory());
            model.put("edit",true);
        }
        model.put("editCategory",addcat);
        return "admin_panel/main";
     }

}
